import {
  getSingerUrl
} from 'api/singer';
import {
  getLyric
} from 'api/song';

import {
  Base64
} from 'js-base64';

export default class Song {
  constructor({
    id,
    mid,
    singer,
    name,
    ablum,
    duration,
    img
  }) {
    this.id = id
    this.mid = mid
    this.singer = singer
    this.name = name
    this.ablum = ablum
    this.duration = duration
    this.img = img
    getSingerUrl(mid).then(res => {
      this.url = res
    })
  }
  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }

    return new Promise((resolve, reject) => {
      getLyric(this.mid).then(res => {
        if(res.code === 0 ){
          this.lyric = Base64.decode(res.lyric);
          resolve(this.lyric)
        }else{
          reject('no song.js')
        }
      })
    })
  }
}

export function createSong(musicData) {
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    ablum: musicData.albumname,
    duration: musicData.interval,
    img: `http://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    // url: 
  })
}

function filterSinger(singe) {
  let ret = []
  if (!singe) {
    return ''
  }
  singe.forEach(s => {
    ret.push(s.name)
  });
  return ret.join('/')
}


export function createDisc(musicData) {
  return new Song({
    id: musicData.id,
    mid: musicData.mid,
    singer: musicData.singer["0"].title,
    name: musicData.name,
    ablum: musicData.album.title,
    duration: musicData.interval,
    img: `http://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.album.mid}.jpg?max_age=2592000`,
    // url: 
  })
}